//
//  DDPIcView.swift
//  swift微博
//
//  Created by whitcast on 16/7/15.
//  Copyright © 2016年 whitcast. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

//添加到的父控件
var superViews: UIView?
//离两边的间距
let sideSpace: CGFloat = 5
//item的间距
let itemSpace: CGFloat = 10
//父控件的宽度
let wid: CGFloat = UIScreen.mainScreen().bounds.size.width
//item的宽度
var singleWidth = (wid - 2.0 * (sideSpace + itemSpace)) / 3.0
//item的高度
var singelheight = singleWidth




class DDPIcView: UICollectionView {
    //cell的重用标识
    let picViewCellIdentifier = "picViewCellIdentifier"
    //图片数量标签
    lazy var lab = UILabel()
    //图片数据
    var pic_urlsArr: [DDPicViewModel]?{
        
        didSet{
            
            if let count = pic_urlsArr?.count where count > 0 {
                
                let size = makeSureTheViewSize(count)
                
                lab.text = "\(count)"
                
                snp_updateConstraints(closure: { (make) -> Void in
                    
                    make.size.equalTo(size)
                })
            }else{
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    make.size.equalTo(CGSizeZero)
                })
            }
            
            self.reloadData()
        }
    }
    
    //构造方法
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let flowOut: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        
        flowOut.itemSize = CGSizeMake(singleWidth, singelheight)
        
        flowOut.minimumLineSpacing = itemSpace
        
        flowOut.minimumInteritemSpacing = itemSpace
        
        super.init(frame: frame, collectionViewLayout:flowOut)
        
        //创建UI
        setUpUI()
        //注册cell
        self.registerClass(DDPIcViewCell.self, forCellWithReuseIdentifier: picViewCellIdentifier)
        //数据源
        dataSource = self
        //颜色
        backgroundColor = UIColor.whiteColor()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   //创建UI
   private func setUpUI(){
        //添加lab
    addSubview(lab)
    //约束
    lab.snp_makeConstraints { (make) -> Void in
        make.center.equalTo(self)
        make.size.equalTo(CGSizeMake(100, 30))
    }
    
    }
    
    //根据图片的数量确定视图的大小
    private func makeSureTheViewSize(count: Int)->CGSize{
        //行数
        let row = (count - 1) / 3 + 1
        //列数
      // let col = 3
        //返回的宽
        let widthes = wid
        
        let heightes = CGFloat(row) * singelheight + CGFloat(row - 1) * itemSpace + 2 * sideSpace
        
       return CGSize(width: widthes - 2 * sideSpace, height: heightes - 2 * sideSpace)
    }

}

// MARK: -- 数据源方法
extension DDPIcView: UICollectionViewDataSource{

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
    
    return pic_urlsArr?.count ?? 0
    
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(picViewCellIdentifier, forIndexPath: indexPath) as! DDPIcViewCell
        
        cell.model = pic_urlsArr![indexPath.row]
        
        return cell
    }

}

// MARK: -- cell

class DDPIcViewCell: UICollectionViewCell{
    //数据模型
    var model: DDPicViewModel?{
        didSet{
            imgView.sd_setImageWithURL(NSURL(string: model!.thumbnail_pic!), placeholderImage: UIImage(named: "timeline_image_placeholder"))
        }
    }
    //图片
    lazy var imgView: UIImageView = {
       let imgVw = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        
        //自适应图片,多出部分删除  UIViewContentModeScaleAspectFill
        imgVw.contentMode = .ScaleAspectFill
        //裁剪多余部分
        imgVw.clipsToBounds = true
        
        return imgVw
    }()
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        
        setUpUI()
    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置UI 和 约束
    func setUpUI() {
        //添加
        contentView.addSubview(imgView)
        //约束
        imgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges).offset(EdgeInsetsZero)
        }
        
//        contentView.snp_makeConstraints { (make) -> Void in
//            make.leading.equalTo(self)
//            make.trailing.equalTo(self)
//            make.top.equalTo(self)
//            make.bottom.equalTo(imgView)
//        }
        
    }
    
    
    
    
    
    
    
}

